﻿using Core.Res;
using UnityEngine;

namespace Core.Scene
{
    public class LoadScenePrefabNode : AbstractSceneNode
    {
        private LoadAwaiter mLoadOp;

        private float targetValue = 0;

        public override void Start()
        {
            IsComplete = false;

            LoadAsync().Coroutine();
        }

        public override void Update()
        {
            base.Update();

            if (mLoadOp == null || IsComplete)
            {
                return;
            }
            targetValue = mLoadOp.Progress;
            if (mLoadOp.State == ELoadState.Complete)
            {
                targetValue = 1;
            }

            if (targetValue >= 0.9f)
            {
                targetValue = 1.0f;
            }

            //CurrValue = targetValue;

            if (mLoadOp.State == ELoadState.Complete && targetValue == 1)
            {
                var go = Object.Instantiate(G.Res.GetAsset(Scene.Conf.PrefabsPath).Obj) as GameObject;
                go.name = Scene.Conf.PrefabsPath;
                //Scene.MainGO = go;
                Scene.MoveGoToMetaScene(go);
                IsComplete = true;
            }
        }

        public override void Finish()
        {

        }

        private async ETTask LoadAsync()
        {
            await G.Res.LoadAsync(Scene.Conf.PrefabsPath);
            mLoadOp = G.Res.GetAsset(Scene.Conf.PrefabsPath) as LoadAwaiter;
        }
    }
}
